/**
 * Project Name:JavaBaseReview
 * Package Name:com.xuwei.base.search
 * author:David
 * Date:2016年5月24日下午7:27:42
 * Copyright (c) 2016, xw123box@126.com All Rights Reserved.
 *
 */
package com.xuwei.base.search;

import com.xuwei.base.tree.BinTreeNode;

/**
 * 红黑树结点类
 * @author David
 * @since 2016年5月24日 下午7:27:42
 * @version 
 * @since JDK 1.6
 */
public class RBTreeNode{
	public static final String RED = "RED";
	public static final String BLACK = "BLACK";
	/**颜色 :true表示红色，false表示黑色,默认为红色*/
	private Boolean color = true;
	/**关键字域*/
	private Object key;
	/**父结点*/
	private RBTreeNode parent;
	/**左子结点*/
	private RBTreeNode lChild;
	/**右子结点*/
	private RBTreeNode rChild;
	/**以该结点为根的子树的高度*/
	private int height;
	/**该结点子孙数（包括当前结点）*/
	private int size;   
	
	public RBTreeNode(Object key) {
		this.key = key;
		height = 0;
		size = 1;
		parent = lChild = rChild = RBTree.NIL;
	}
	
	public RBTreeNode(Object e, String color) {
		this(e);
		this.color = color==RED?true:false;
	}
	
	@Override
	public String toString() {
		return this.color==true?"Red"+key:"Black"+key;
	}

	public boolean isRed(){
		return color;
	}

	public Boolean getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color==RED?true:false;
	}

	public Object getKey() {
		return key;
	}

	public void setKey(Object key) {
		this.key = key;
	}

	public RBTreeNode getParent() {
		return parent;
	}

	public void setParent(RBTreeNode parent) {
		this.parent = parent;
	}

	public RBTreeNode getlChild() {
		return lChild;
	}

	public void setlChild(RBTreeNode lChild) {
		this.lChild = lChild;
	}

	public RBTreeNode getrChild() {
		return rChild;
	}

	public void setrChild(RBTreeNode rChild) {
		this.rChild = rChild;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}
	
	//判断是否有左子树
	public boolean hasLChild(){
		return lChild!=RBTree.NIL;
	}
	//判断是否有右子树
	public boolean hasRChild(){
		return rChild!=RBTree.NIL;
	}
	//判断是否有父亲
	public boolean hasParent(){
		return parent!=RBTree.NIL;
	}
	//判断是否为某结点的左子树
	public boolean isLChild(){
		return hasParent()&&this==parent.lChild;
	}
	//判断是否为某结点的右子树
	public boolean isRChild(){
		return hasParent()&&this==parent.rChild;
	}
}
